package persist

func GetDriverGnc[T IxDriver](driver T, cfgName string) (*Connect, T) {
	sourceKey := fmtSourceKey(driver.DriverName(), cfgName)
	if value, exist := dbs.connMap[sourceKey]; exist {
		if db, ok := value.IxDriver.(T); ok {
			return dbs.connMap[sourceKey], db
		} else {
			var zero T
			return nil, zero
		}
	} else {
		conn := new(Connect).InitConnect(driver, cfgName)
		dbs.connMap[sourceKey] = conn
		return conn, driver
	}
}

func GetOexGnc[T IxOpsExt, D IxDriver](oex T, driver D, cfgName string) (T, D) {
	sourceKey := fmtSourceKey(driver.DriverName(), cfgName)

	conn, d := GetDriverGnc(driver, cfgName)

	if value, exist := dbs.oexMap[sourceKey]; exist {
		if o, ok := value.(T); ok {
			return o, d
		} else {
			var zero T
			return zero, d
		}
	} else {
		oex.InitOpsExt(conn, d)
		dbs.oexMap[sourceKey] = oex
		return oex, d
	}
}

func GetOexDefault() *OexSqlxAlpha {
	sqlxAlpha, _ := GetOexGnc(new(OexSqlxAlpha), new(SqlxDriver), "DB")
	return sqlxAlpha
}
